﻿using PythonModPacker.helper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PythonModPacker
{
    class ProjectUtil
    {
        private String rootPath;
        private String projectName;
        private string behJsonTemplate = "{\"format_version\": 1,\"header\": {\"description\": \"\",\"name\": \"behavior_pack\",\"uuid\": \"{uuid1}\",\"version\": [0,0,1]},\"modules\": [{\"description\": \"\",\"type\": \"data\",\"uuid\": \"{uuid2}\",\"version\": [0,0,1]}]}";
        private string resJsonTemplate = "{\"format_version\": 1,\"header\": {\"description\": \"\",\"name\": \"resource_pack\",\"uuid\": \"{uuid1}\",\"version\": [0,0,1]},\"modules\": [{\"description\": \"\",\"type\": \"resources\",\"uuid\": \"{uuid2}\",\"version\": [0,0,1]}]}";
        private bool isServerMod;

        public override bool Equals(object obj)
        {
            return obj is ProjectUtil util &&
                   rootPath == util.rootPath &&
                   projectName == util.projectName &&
                   isServerMod == util.isServerMod;
        }

        public override int GetHashCode()
        {
            var hashCode = 1923525777;
            hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(rootPath);
            hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(projectName);
            hashCode = hashCode * -1521134295 + isServerMod.GetHashCode();
            return hashCode;
        }

        public ProjectUtil(string rootPath, string projectName, bool isServerMod)
        {
            this.rootPath = rootPath;
            this.projectName = projectName;
            this.isServerMod = isServerMod;
        }

        public void GenerateFolder()
        {
            DirectoryInfo worlds = new DirectoryInfo(Path.Combine(rootPath, projectName, "worlds", "level"));
            worlds.Create();
            DirectoryInfo behavior_packs = new DirectoryInfo(Path.Combine(rootPath, projectName, "behavior_packs",projectName, projectName+"Beahvior"));
            behavior_packs.Create();
            DirectoryInfo entities = new DirectoryInfo(Path.Combine(behavior_packs.FullName, "entities"));
            entities.Create();
            StreamWriter behManifest = new StreamWriter(Path.Combine(behavior_packs.FullName, "manifest.json"));
            var json = behJsonTemplate;
            json = json.Replace("{uuid1}", UUIDHelper.NewUuidString()).Replace("{uuid2}", UUIDHelper.NewUuidString());
            behManifest.WriteLine(json);
            behManifest.Close();
            DirectoryInfo resource_packs = new DirectoryInfo(Path.Combine(rootPath, projectName, "resource_packs", projectName, projectName + "Res"));
            resource_packs.Create();
            StreamWriter resManifest = new StreamWriter(Path.Combine(resource_packs.FullName, "manifest.json"));
            json = resJsonTemplate;
            json = json.Replace("{uuid1}", UUIDHelper.NewUuidString()).Replace("{uuid2}", UUIDHelper.NewUuidString());
            resManifest.WriteLine(json);
            resManifest.Close();
            DirectoryInfo develeop_mods = new DirectoryInfo(Path.Combine(rootPath, projectName, "developer_mods", projectName, projectName + "Dev"));
            develeop_mods.Create();
        }

    }
}
